package com.lrf.freeshipping.filter;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.lrf.freeshipping.bean.PageBean;
import com.lrf.freeshipping.jdbc.JDBCOption;
import com.lrf.freeshipping.jdbc.SQLConstant;

@WebFilter("/index.jsp")
public class IndexFilter implements Filter{

	@Override
	public void destroy() {
		
	}

	@SuppressWarnings("rawtypes")
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		HttpSession session = ((HttpServletRequest)request).getSession();
		PageBean pageBean = (PageBean) session.getAttribute("pageBean");		
		if(pageBean ==null) pageBean = new PageBean();
		List list = JDBCOption.query(SQLConstant.queryAllProduct);
		int productNumber = list.size();
		int pageSize = 10;
		pageBean.setCurrentPage(1);
		pageBean.setPageSize(pageSize);
	    pageBean.setPageUnmber(productNumber%pageSize==0?productNumber/pageSize:productNumber/pageSize+1);
		pageBean.setProductNumber(productNumber);	
		session.setAttribute("pageBean", pageBean);
		chain.doFilter(request, response);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		
	}

}
